'use strict';

exports.index = async ctx => {
  ctx.w_validate_page();
  const defaultStartDate = new Date(0).toISOString();
  const defaultEndDate = new Date().toISOString();
  ctx.validate({
    startDate: {
      type: 'w-date',
      required: false,
      default: defaultStartDate,
    },
    endDate: {
      type: 'w-date',
      required: false,
      default: defaultEndDate,
    },
    conditions: {
      type: 'string',
      required: false,
    },
    selKey: {
      type: 'string',
      required: false,
    },
    reverse: {
      type: 'enum',
      values: [ 'true', 'false' ],
      required: false,
      default: 'true',
    },
  }, ctx.query);
  const data = await Promise.all([ ctx.service.logs.serverError.list(), ctx.service.logs.serverError.count() ]);
  const resData = {
    list: data[0],
    total: data[1],
    pageSize: ctx.query.pageSize,
    pageNum: ctx.query.pageNum,
  };
  ctx.sendSuccess(resData);
};

exports.destroy = async ctx => {
  ctx.validate({
    id: {
      type: 'mongo-id',
    },
  }, ctx.params);
  ctx.sendSuccess(await ctx.service.logs.serverError.destroy());
};
